package com.lisn.recyclerviewlib;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

/**
 * @author : lishan
 * @e-mail : cnlishan@163.com
 * @date : 2020/7/10 3:32 PM
 * @desc :
 */
public class NotesAdapter extends RecyclerView.Adapter<NotesAdapter.ViewHolder> {
    private static final String TAG = NotesAdapter.class.getSimpleName();

    private List<Notes> list;//Notes是我们上面布局所对应的类，这里就不贴了
    private Context context;//从acticity中获取context

    private OnItemLongClickListener onItemClickListener;//实现长按监听的接口（自己实现）
    private OnItemClickListener onItemClick;//实现单点监听的接口（自己实现）
    private Boolean longclick = false;//判断目前recyclerview的状态是处于长按后还是长按前（防止前后的单点时间冲突）
    private Boolean choose = true;//长按状态是判断当前item是否被选中

    //用来在activity中初始化适配器
    public NotesAdapter(List<Notes> list, Context context) {
        this.list = list;
        this.context = context;
    }

    //这里就不详述了（可以参照我之前的RecyclerView简单使用）
    @Override
    public NotesAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.notes_card, null, false);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(layoutParams);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final NotesAdapter.ViewHolder holder, final int position) {
        final Notes notes = list.get(position);
        holder.notes_content_part.setText(notes.getNotes_content_part());
        if (!notes.isChoose()) {
            holder.check_box.setImageResource(R.drawable.circhose);//点击后更换图为未选中
        } else {
            holder.check_box.setImageResource(R.drawable.circhosetouch);
        }

        if (longclick) {
            holder.check_box.setVisibility(View.VISIBLE);//如果当前为长按状态则让长按选择图片显示
        } else {
            holder.check_box.setVisibility(View.GONE);//否则让其消失
        }

        holder.root_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*
                 * 长按状态的逻辑
                 * if语句中是根据当前长按图片所显示的图来判断是否被选中
                 * */
                if (longclick) {
                    if (notes.isChoose()) {
                        holder.check_box.setImageResource(R.drawable.circhose);//点击后更换图为未选中
                        choose = false;//将choose设置为未选中态
                        notes.setChoose(false);
                    } else {
                        holder.check_box.setImageResource(R.drawable.circhosetouch);
                        choose = true;
                        notes.setChoose(true);
                    }
                    onItemClick.OnItemClick(position, choose, list);//将当前选中的位置以及状态通过自定义接口在activity中使用
                } else if (!longclick) {  //正常状态的逻辑
                    Toast.makeText(context, "点了一下", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout root_view;
        TextView notes_content_part;
        TextView notes_time;
        ImageView check_box;

        public ViewHolder(View itemView) {
            super(itemView);
            notes_content_part = itemView.findViewById(R.id.notes_content_part);
            notes_time = itemView.findViewById(R.id.notes_time);
            check_box = itemView.findViewById(R.id.check_box);
            root_view = itemView.findViewById(R.id.root_view);
            //用root_view的长按点击来实现整个item的长按点击
            root_view.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    longclick = longclick ? false : true;//令recyclerview处于长按状态
                    onItemClickListener.OnItemLongClick(longclick);//实现自定义长按
                    return true;
                }
            });

        }

    }

    //删除Notes
    public void removeNotes(int position) {
        list.remove(position);
//        notifyDataSetChanged();
        notifyItemRemoved(position);
        if (position != list.size()) {
            notifyItemRangeChanged(position, list.size() - position);
        }
    }

    //定义长按接口
    public interface OnItemLongClickListener {
        void OnItemLongClick(Boolean longclick);
    }

    //定义单点接口
    public interface OnItemClickListener {
        void OnItemClick(int x, boolean adro, List<Notes> list);//这里的List<Notes> list是因为要在数据库中删除传的，没有需求的小伙伴可以不用
    }

    //定义长按接口的实现
    public void setOnItemClickListener(OnItemClickListener onItemClick) {
        this.onItemClick = onItemClick;
    }

    //定义单点接口的实现
    public void setOnItemLongClickListener(OnItemLongClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
